package com.galleryviewpager.c_circleprecent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;
import com.galleryviewpager.R;

import java.lang.invoke.MethodHandleInfo;
import java.util.Map;

public class TestCirclePercentView extends View {
    private Context mContext;

    private int mWidth, mHeight;
    private int mBigColor;
    private int mSmallColor;
    private int mAngleColor;
    private float mStrokeWidth;
    private int mPercent;

    private RectF rectF = new RectF();

    private int mCircleLength;

    private Paint mPaint = new Paint();

    public TestCirclePercentView(Context context) {
        this(context, null);
    }

    public TestCirclePercentView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestCirclePercentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TestCirclePercentView, defStyleAttr, 0);

        mBigColor = typedArray.getColor(R.styleable.TestCirclePercentView_testCircleBigColor, Color.GREEN);
        mSmallColor = typedArray.getColor(R.styleable.TestCirclePercentView_testCircleSmallColor, Color.BLACK);
        mAngleColor = typedArray.getColor(R.styleable.TestCirclePercentView_testCircleAngleColor, Color.RED);

        mStrokeWidth = typedArray.getDimension(R.styleable.TestCirclePercentView_testCircleStokeWidth, DensityUtils.dip2px(mContext, 40));
        mPercent = typedArray.getInt(R.styleable.TestCirclePercentView_testCircleRadius, 50);


        typedArray.recycle();

        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(20f);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = DensityUtils.dip2px(mContext, 400);
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = DensityUtils.dip2px(mContext, 400);
        }
        mCircleLength = Math.min(mWidth, mHeight) / 2 - 10;

        rectF.set(mWidth / 2 - mCircleLength, mHeight / 2 - mCircleLength, mWidth / 2 + mCircleLength, mHeight / 2 + mCircleLength);
        setMeasuredDimension(mWidth, mHeight);

        Log.e(this.getClass().getSimpleName(), " mCircleLength : " + mCircleLength + " mStrokeWidth : " + mStrokeWidth);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        mPaint.setColor(mBigColor);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mCircleLength, mPaint);

        mPaint.setColor(mAngleColor);
        canvas.drawArc(rectF, 0, 100, true, mPaint);

        mPaint.setColor(mSmallColor);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mCircleLength - mStrokeWidth, mPaint);


    }
}
